<?php
include_once 'field.class.php';

class field_checkbox extends field
{
    public $error;
    protected $fieldname = '是否选择框';

    public function __construct($field='')
    {
        parent::__construct($field);
        $this->field_type = 'tinyint';
    }

    /**
     * 生成设置项
     * @author menghao
     * @param <string> $data
     * @return <string>
     */
    function settings($data='')
    {
        $data = unserialize($data);
        $data['label'] = isset($data['label']) ? $data['label'] : '';
        $data['yes'] = isset($data['yes']) ? $data['yes'] : '1';
        $data['no'] = isset($data['no']) ? $data['no'] : '0';
        $data['default'] = isset($data['default']) ? $data['default'] : '0';
        
        $setting = '';
        $setting .= '<label>提示文本：'.form::text('settings[label]', 'setting_label', $data['label'], 20).'</label> ';
        $setting .= '<label>是：'.form::text('settings[yes]', 'setting_yes', $data['yes'], 2).'</label> ';
        $setting .= '<label>否：'.form::text('settings[no]', 'setting_no', $data['no'], 2).'</label> ';
        $setting .= '<label>默认：'.form::text('settings[default]', 'setting_default', $data['default'], 2).'</label> ';

        return $setting;
    }


    /**
     * 用户输入格式转换为存储格式
     * @param <mixed> $data
     * @return <string>
     */
    function get_data($data)
    {
        return empty($data) ? 0 : 1;
    }


    function create_form($value='')
    {
        if($value === '')
        {
            $value = $this->field_data['settings']['default'];
        }
        $field = $this->field_data['field'];
        $label = $this->field_data['settings']['label'];
        $yes = $this->field_data['settings']['yes'];
        $no = $this->field_data['settings']['no'];
        $checked = empty($value) ? '' : 'checked';
        $form = form::check_yesno("data[$field]", "data_$field", $label, $yes, $no, $value);
        return $form;
    }
}
?>